<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

/**
 * 课程章节管理
 *
 * @icon fa fa-circle-o
 */
class Chapter extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('subject.Chapter');
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            //设置过滤方法
            $this->request->filter(['strip_tags', 'trim']);

            //如果发送的来源是 Selectpage，则转发到 Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            //通过fastadmin封装函数处理前端发过来的参数
            [$where, $sort, $order, $offset, $limit] = $this->buildparams();

            $list = $this->model
                ->where($where)          //搜索条件
                ->order($sort, $order)         //$sort排序字段,$order降序还是升序
                ->paginate($limit);    //调用分页函数

            $result = ['total' => $list->total(), 'rows' => $list->items()];      //返回排序总数total()和排序数据items()
            return json($result);    //返回数据
        }
        return $this->fetch();
    }
}
